#coding:utf-8
import time
import re


'''
#案例
#今天 11:09 来自微博 weibo.com
#07月25日 16:32 来自微博 weibo.com
#2013-12-11 08:59:18 来自微博 weibo.com
#5分钟前 来自iPhone 7


str1 = '今天 11:09 来自微博 weibo.com'
str2 = '07月25日 16:32 来自微博 weibo.com'



pattern1 = r'\d{2}:\d{2}'
pattern2 = r'\d{2}月\d{2}日.*?\d{2}:\d{2}'
# 微博数

#guid = re.findall(pattern2, str2, re.S | re.M)
if len(guid):
  value = guid[0]
  print value

pat = '2013-12-11 08:59:18'


pat1 = r'今天.*?(\d{2}):(\d{2}).*'
matchObj1 = re.match(pat1, str1, re.M | re.I)

if matchObj1:
    print "matchObj.group(1) : ", matchObj1.group(1)
    print "matchObj.group(2) : ", matchObj1.group(2)
else:
    print "No match!!"
pat2 = r'.*?(\d{2})月(\d{2})日.*?(\d{2}):(\d{2}).*?'
matchObj2 = re.match(pat2, str2, re.M | re.I)
if matchObj2:
    print "matchObj.group(1) : ", matchObj2.group(1)
    print "matchObj.group(2) : ", matchObj2.group(2)
    print "matchObj.group(3) : ", matchObj2.group(3)
    print "matchObj.group(4) : ", matchObj2.group(4)
else:
    print "No match!!"

str3 = '0分钟前 来自iPhone 7'
pat3 = r'.*?(\d+)分钟前.*?'
matchObj3 = re.match(pat3, str3, re.M | re.I)
if matchObj3:
    print "matchObj.group(1) : ", matchObj3.group(1)
else:
    print "No match!!"



str4 = '2013-12-11 08:59:18 来自微博 weibo'
pat4 = r'.*?(\d{4})-(\d{2})-(\d{2}).*?(\d{2}):(\d{2}):(\d{2}).*?'


now_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
matchObj4 = re.match(pat4, now_time, re.M | re.I)
if matchObj4:
    year = matchObj4.group(1)
    month = matchObj4.group(2)
    day =   matchObj4.group(3)
    hour = matchObj4.group(4)
    minute = matchObj4.group(5)
    second = matchObj4.group(6)


    print "year : ", year
    print "month : ", month
    print "day : ", day
    print "hour : ", hour
    print "minute : ", minute
    print "second : ", second
else:
    print "No match!!"

guid = re.findall(pat4, str4, re.S | re.M)
if len(guid):
  value = guid[0]
  print value
  #print 'time = ' + time.strptime(value, "%Y-%m-%d %H:%M:%S")

timestr = "time2009-12-14"
t = time.strptime(timestr, "time%Y-%m-%d")
'''

#now_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
#print type(now_time)
#print now_time
#print now_time





class timeutils:
    year = ""
    month = ""
    day = ""
    hour = ""
    minute = ""
    second = ""



    def processTime(self, str_input_time):


        '''print '分钟前' + str(str_input_time.find('分钟前'))
        print '今天' + str(str_input_time.find('今天'))
        print '月' + str(str_input_time.find('月'))
        print '年' + str(str_input_time.find('-'))'''
        weibo_time = ''
        if str_input_time.find('分钟前') >= 0:
            weibo_time = self.handle_minute(str_input_time)
        elif str_input_time.find('今天') >= 0:
            weibo_time = self.handle_today(str_input_time)
        elif str_input_time.find('月') >= 0:
            weibo_time = self.handle_month_and_day(str_input_time)
        elif str_input_time.find('-') >= 0:
            weibo_time = self.handle_all(str_input_time)
        return weibo_time




    def get_nowtime(self):

        now_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))

        pat = r'.*?(\d{4})-(\d{2})-(\d{2}).*?(\d{2}):(\d{2}):(\d{2}).*?'
        matchObj = re.match(pat, now_time, re.M | re.I)

        if matchObj:
            timeutils.year = matchObj.group(1)
            timeutils.month = matchObj.group(2)
            timeutils.day = matchObj.group(3)
            timeutils.hour = matchObj.group(4)
            timeutils.minute = matchObj.group(5)
            timeutils.second = matchObj.group(6)

            '''print "year : ", timeutils.year
            print "month : ", timeutils.month
            print "day : ", timeutils.day
            print "hour : ", timeutils.hour
            print "minute : ", timeutils.minute
            print "second : ", timeutils.second'''
        else:
            print "No match!!"

    def handle_minute(self,str_time):

            #2017-07-27 09:04:15
            #str3 = '0分钟前 来自iPhone 7'
            pat_minute = r'.*?(\d+)分钟前.*?'
            matchObj3 = re.match(pat_minute, str_time, re.M | re.I)
            if matchObj3:
                print "matchObj.group(1) : ", matchObj3.group(1)
            else:
                print "No match!!"

    def handle_all(self, str_time):
        pat_all = r'\d{4}-\d{2}-\d{2}.*?\d{2}:\d{2}:\d{2}'
        guid = re.findall(pat_all, str_time, re.S | re.M)
        weibo_time = ''
        if len(guid):
            weibo_time = guid[0]
        return weibo_time

    def handle_today(self, str_time):
            self.get_nowtime()
            #str = '今天 11:09 来自微博 weibo.com'
            pat_today = r'.*?今天.*?(\d{2}):(\d{2}).*?'

            matchObj = re.match(pat_today, str_time, re.M | re.I)
            weibo_time = ''
            if matchObj:
                hour =  matchObj.group(1)
                minute =  matchObj.group(2)
                weibo_time = timeutils.year + '-' + timeutils.month + '-' + timeutils.day + ' ' + hour + ":" + minute + ":" + timeutils.second

            else:
                print "No match!!"

            return weibo_time


    def handle_minute(self, str_time):

           self.get_nowtime()
           #str_minute = '0分钟前 来自iPhone 7'
           pat_minute = r'.*?(\d+)分钟前.*?'
           matchObj = re.match(pat_minute, str_time, re.M | re.I)
           weibo_time = ''
           if matchObj:
               minute =  matchObj.group(1)
               t = time.time() - int(minute) * 60
               weibo_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(t))
           else:
               print "No match!!"


           return weibo_time
    def handle_month_and_day(self, str_time):

            self.get_nowtime()
            #str = '07月25日 16:32 来自微博 weibo.com'
            pat__month_and_day = r'.*?(\d{2})月(\d{2})日.*?(\d{2}):(\d{2}).*?'
            #2017-07-27 09:04:15
            matchObj = re.match(pat__month_and_day, str_time, re.M | re.I)
            weibo_time = ''
            if matchObj:
                month =  matchObj.group(1)
                day = matchObj.group(2)
                hour = matchObj.group(3)
                minute = matchObj.group(4)
                weibo_time = timeutils.year + '-' + month + '-' + day + ' ' + hour + ":" + minute + ":" + timeutils.second
            else:
                print "No match!!"

            return weibo_time

'''print  timeutils(0).processTime('07月25日 16:32 来自微博 weibo.com')
print  timeutils(0).processTime('今天 11:09 来自微博 weibo.com')
print  timeutils(0).processTime('2013-12-11 08:59:18 来自微博 weibo.com')'''
print  timeutils().processTime('07月25日 16:32 来自微博 weibo.com')
